<?php
class RCMSClassLoader{
	static protected $dirs;
	static protected $subdirs = array(
		'ctrl',
		'dao',
	);

	static public function register() {
		spl_autoload_register(array(new self, 'loadClass'));
	}
	static public function registerDir($dir){
		RCMSClassLoader::$dirs[] = $dir;
	}
	static public function loadClass($class) {
		if (0 === strpos($class, 'Twig')) {
			Twig_Autoloader::autoload($class);
			return;
		}
		foreach (RCMSClassLoader::$subdirs as $subdir) {
			$pos = strlen($class) -  strlen($subdir);
			if (( $pos > 0 ) && (stripos($class, $subdir, $pos)!==false)) {
				$class = $subdir.'/'.$class;
				break;
			}
		}
		foreach (RCMSClassLoader::$dirs as $dir) {
			$file = $dir . '/' . $class . '.php';
			if (is_readable($file)) {
				require_once $file;
				return;
			}
		}
		throw new RuntimeException('Not found class for '.$class);
	}
}
